$(document).ready(
    function(){
        
        var UI = {};
        (function (UI) {
            
            UI.start = function(){

            };
            
        }(UI));
        var Helpers = {};
        (function (Helpers) {
            
            
            
        }(Helpers));
        
        var Actions = {};
        (function (Actions) {
            
            Actions.logIn = function(args) {
                console.log("%c runSQL logIn", "color: green");
                var dfd = new $.Deferred();
                $.ajax({
                    type: "POST",
                    url: "../resources/helpers/logIn.php",
                    data: args,
                    cache: false,
                    success: function(data){
                        console.log("%c Returned from logIn:","color: green");
                        console.log(data);
                        dfd.resolve();
                    }
                });
                return dfd.promise();
            };
            
        }(Actions));
        
        var Controller = {};
        (function (Controller) {
            
            function checkEmail(){
                var isEmailValid = validateEmail($('#logInEmail').val());
                var isPasswordValid = validatePassword($('#logInPassword').val());
                if(isEmailValid === -1){ //email is empty
                    $("#email-error").addClass("hidden");
                    $("#email-check").addClass("hidden");
                    $("#confirmLoginBtn").addClass("disabled");
                    return;
                }
                if(isEmailValid === -2){ //email is not valid
                    $("#email-error").removeClass("hidden");
                    $("#email-check").addClass("hidden");
                    $("#confirmLoginBtn").addClass("disabled");
                    return;
                }

                $("#email-error").addClass("hidden");
                $("#email-check").removeClass("hidden");

                if(isPasswordValid < 0){ //password is empty or not valid
                    $("#confirmLoginBtn").addClass("disabled");
                } else{
                    $("#confirmLoginBtn").removeClass("disabled");
                }
            }
            
            function checkPassword(){
                var isPasswordValid = validatePassword($('#logInPassword').val());
                var isEmailValid = validateEmail($('#logInEmail').val());
                if(isPasswordValid === -1){ //password is empty
                    $("#pw-error").addClass("hidden");
                    $("#pw-check").addClass("hidden");
                    $("#confirmLoginBtn").addClass("disabled");
                    return;
                }

                if(isPasswordValid === -2){ //password is shorter than 8 characters
                    $("#pw-error").removeClass("hidden");
                    $("#pw-check").addClass("hidden");
                    $("#confirmLoginBtn").addClass("disabled");
                    return;
                }

                $("#pw-error").addClass("hidden");
                $("#pw-check").removeClass("hidden");

                if(isEmailValid < 0){ //email is empty or not valid
                    $("#confirmLoginBtn").addClass("disabled");
                } else{
                    $("#confirmLoginBtn").removeClass("disabled");
                }
            }
            
            function logInClicked(){
                var args = {
                    logInEmail : $('#logInEmail').val(),
                    logInPassword : $('#logInPassword').val()
                };
                
                $.when(Actions.logIn(args)).then(
                    function(returnItems){
                        console.log("Returned with:");
                        console.log(returnItems);
                        location.reload();
                    }
                );
            }
            
            Controller.init= function(){
                $("#confirmLoginBtn").unbind("click").bind("click", function(){
                    if(!$("#confirmLoginBtn").hasClass("disabled")){
                        logInClicked();
                    };
                });
                
                $("#logInEmail").unbind("input propertychange keyup change").bind("input propertychange keyup change", function() {
                    checkEmail();
                });
                
                $("#logInPassword").unbind("input propertychange keyup change").bind("input propertychange keyup change", function() {
                    checkPassword();
                });
                
                $(document).keyup(function(e) {
                    if($("#outOfLogInDialog").css("display") !== "none"){
                        if(e.keyCode === 13){
                            var isEmailValid = validateEmail($('#logInEmail').val());
                            var isPasswordValid = validatePassword($('#logInPassword').val());
                            if(isEmailValid === 1 && isPasswordValid === 1){
                                $("#confirmLoginBtn").click();
                            }
                        } else if(e.keyCode === 27){
                            $("#cancelLoginBtn").click();
                        }
                    }
                });
            };
            
        }(Controller));	
        
        UI.start();
        Controller.init();
    }
);